﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api.Domain;
using System.Configuration;
using System.Net.Json;

namespace SC_BLL
{
    public class Showcase
    {
        ITopClient c;

        public Showcase() { 
            c = new DefaultTopClient(Config.ServerURL,Config.Appkey,Config.Secret);
        }

        public string GetRemainShowcase()
        {
            string reValue = string.Empty;

            ShopRemainshowcaseGetRequest req = new ShopRemainshowcaseGetRequest();
            ShopRemainshowcaseGetResponse res = c.Execute(req,Client.Session);

            if (!res.IsError)
            {
                reValue = res.Shop.RemainCount.ToString();
            }

            return reValue;
        }

        public int AddItemRecommend(string num_iids)
        {
            int count = 0;

            ItemRecommendAddRequest req = new ItemRecommendAddRequest();

            foreach (string num_iid in num_iids.Split(','))
            {
                req.NumIid = int.Parse(num_iid);
                ItemRecommendAddResponse res = c.Execute(req);
                if (res.IsError)
                {

                }
                else
                {
                    count++;
                }
            }

            return count;
        }

        public string GetItemsOnsale(string title,int currPage)
        {
            string reValue = string.Empty;

            ItemsOnsaleGetRequest req = new ItemsOnsaleGetRequest();
            req.Fields = "pic_url,title,price,list_time,valid_thru,has_showcase";
            req.Q = title;
            req.PageSize = 10;
            req.PageNo = currPage;
            
            ItemsOnsaleGetResponse res = c.Execute(req,Client.Session);

            if (res.IsError)
            {
                    JsonObjectCollection joc = new JsonObjectCollection();
                    joc.Add(new JsonStringValue("errcode",res.ErrCode));
                    string msg = string.Empty;
                    if (!string.IsNullOrEmpty(res.SubErrMsg))
                    {
                        msg = res.SubErrMsg;
                    }
                    else {
                        msg = res.ErrMsg;
                    }
                    joc.Add(new JsonStringValue("errmsg", msg));
                    JsonUtility.GenerateIndentedJsonText = true;
                    reValue = joc.ToString();
            }
            else
            {
                JsonObjectCollection jocRoot = new JsonObjectCollection();
                JsonArrayCollection jacItems = new JsonArrayCollection("items");
                foreach (Item item in res.Items)
                {
                    JsonObjectCollection joc = new JsonObjectCollection();
                    joc.Add(new JsonStringValue("picurl",item.PicUrl));
                    joc.Add(new JsonStringValue("title", item.Title));
                    TimeSpan ts1 = new TimeSpan(DateTime.Parse(item.ListTime).Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime.Parse(item.ListTime).AddDays(double.Parse(item.ValidThru.ToString())).Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    string tmpTime = (ts.Days==0?"":ts.Days.ToString()+"天") + (ts.Hours==0?"":ts.Hours+"时") + (ts.Minutes==0?"":ts.Minutes.ToString()+"分");
                    joc.Add(new JsonStringValue("detime", tmpTime));
                    joc.Add(new JsonStringValue("price", item.Price+"元"));
                    joc.Add(new JsonStringValue("hasshowcase", item.HasShowcase?"已推荐":"未推荐"));
                    jacItems.Add(joc);
                }
                jocRoot.Add(jacItems);
                JsonUtility.GenerateIndentedJsonText = true;
                reValue = jocRoot.ToString();
            }
            
            return reValue;
        }


    }
}
